package com.aigou.admin.service.impl;

import com.aigou.admin.dao.ProductDAO;
import com.aigou.admin.dao.impl.ProductDAOImpl;
import com.aigou.admin.entity.PageEntity;
import com.aigou.admin.entity.Product;
import com.aigou.admin.service.ProductService;

import java.util.List;

public class ProductServiceImpl implements ProductService {

    private final ProductDAO productDAO = new ProductDAOImpl();

    @Override
    public List<Product> getAllProducts() {
        return productDAO.getAllProducts();
    }

    @Override
    public Product getProductById(String id) {
        return productDAO.getProductById(id);
    }

    @Override
    public int addProduct(Product product) {
        return productDAO.addProduct(product);
    }

    @Override
    public int deleteProduct(String id) {
        return productDAO.deleteProduct(id);
    }

    @Override
    public int updateProduct(Product product) {
        return productDAO.updateProduct(product);
    }

    @Override
    public PageEntity<Product> findProductPage(Integer current, Integer pageSize) {
        //查询总记录数
        int total = productDAO.selectProductCount();
        //查询当前页数据
        List<Product> products = productDAO.selectProductLimits((current - 1) * pageSize, pageSize);
        //包装数据发送给前端
        PageEntity<Product> pageEntity = new PageEntity<>();
        pageEntity.setCurrent(current);
        pageEntity.setPageSize(pageSize);
        pageEntity.setTotal(total);
        pageEntity.setRecords(products);
        return pageEntity;
    }
}

